$(document).ready(function() {
        var columnCounter = 0;
        var columns = [];
        var numColumns = $('.commentContainer h1').length;
        var hiddenBooks;
        var hiddenVideos;
        var hiddenNotes;
        var hiddenOther;
        var append_filter_buttons_and_search;
   
// -------------------------------------------------------------------------------------------------
// ------------------------- HTML APPENDAGE FOR BUTTONS AND SEARCH BOX -----------------------------
// -------------------------------------------------------------------------------------------------
  
        append_filter_buttons_and_search = 
                  '<div> <span class="myTooltip" id="toolTipSearch">'
                        + '<input type="text" id="search-text" placeholder="search" />' 
                        + '<span><!-- enter in this span the text to go in the tool tip box --><span>Filter resources by keyword </span></span></span>'
                        + '<div id="searchContainer"></div>'
                + '</div>'
                + '<a class="clearSearch"></a>'
                
                + '<div class="myTooltip" id="toolTipBooks">'
                        + '<div id="filterBooks" class="filterButton">Books</div>'
                        + '<span><!-- enter in this span the text to go in the tool tip box -->'
                        + '<span style="color: #01DF3A; font-weight:bold;">Green</span> = some books are <span style="font-weight:bold;">visible</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="color: red; font-weight:bold;">Red</span> = all books are <span style="font-weight:bold;">hidden</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="font-style:italic;">Click button to toggle show/hide</span></span>'
                + '</div>'
        
                + '<div class="myTooltip" id="toolTipNotes">'
                        + '<div id="filterNotes" class="filterButton">Notes</div>'
                        + '<span><!-- enter in this span the text to go in the tool tip box -->'
                        + '<span style="color: #01DF3A; font-weight:bold;">Green</span> = some notes are <span style="font-weight:bold;">visible</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="color: red; font-weight:bold;">Red</span> = all notes are <span style="font-weight:bold;">hidden</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span> '
                        + '<span style="font-style:italic;">Click button to toggle show/hide</span></span>'
                + '</div>'
                
                + '<div class="myTooltip" id="toolTipOther">'
                        + '<div id="filterOther" class="filterButton">Other</div>'
                        + '<span><!-- enter in this span the text to go in the tool tip box -->'
                        + '<span style="color: #01DF3A; font-weight:bold;">Green</span> = some other are <span style="font-weight:bold;">visible</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="color: red; font-weight:bold;">Red</span> = all other are <span style="font-weight:bold;">hidden</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="font-style:italic;">Click button to toggle show/hide</span></span>'
                + '</div>'
                
                + '<div class="myTooltip" id="toolTipVideos">'
                        + '<div id="filterVideos" class="filterButton">Videos</div>'
                        + '<span><!-- enter in this span the text to go in the tool tip box -->'
                        + '<span style="color: #01DF3A; font-weight:bold;">Green</span> = some videos are <span style="font-weight:bold;">visible</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="color: red; font-weight:bold;">Red</span> = all videos are <span style="font-weight:bold;">hidden</span>'
                        + '<br /><span style="line-height: 0.3"><br /></span>'
                        + '<span style="font-style:italic;">Click button to toggle show/hide</span></span>'
                + '</div>'

                + '<div id="filterContainer"></div>';
                
        $('body').append(append_filter_buttons_and_search);     
        
        
// -------------------------------------------------------------------------------------------------
        $('.WikispacesStream').hide();
        
// -------------------------------------------------------------------------------------------------
// ----------------------------------------- CSS FORMATTING ----------------------------------------
// -------------------------------------------------------------------------------------------------
        //defining our big string for the toc
        bigLongString = '';

        stringForStyles = '<style type="text/css">';
        
        stringForStyles += '*{font-family:sans-serif; }';

        stringForStyles += '.commentContainer h1 {font-size: 2.2em !important;  margin: 0 0 3px 0; }';
        stringForStyles += '.commentContainer h2{font-size:1.6em !important; margin-bottom:-0.5em; }';
        stringForStyles += '.commentContainer h3 {font-size: 1.6em !important; margin: 0 0 1px 0;}   ';
        stringForStyles += '.commentContainer h4{font-size:1.4em !important;}';
        stringForStyles += '.commentContainer h5 {font-size: 1.4em !important; margin: 0 0 2px 2.5em;} ';
        stringForStyles += '.commentContainer h6{font-size:1.2em !important; margin: 0 0 3px 6.0em;}';

        stringForStyles += '.my_link_content li{ font-size: 1.0em; line-height: 1.5; }';

        stringForStyles += 'a.backToTop:link, a.backToTop:visited {padding:0 10px 0 10px; font-size: 1em; color:white !important; background-color:#151515; display:block; text-decoration:none; width 15px; height: 40px; line-height: 40px; position:fixed; bottom:10px; right:20px; z-index:1666 !important; opacity:0.4; border-radius:5px;} ';
        stringForStyles += 'a.backToTop:hover {background-color:#08088A;}';

        stringForStyles += '#searchContainer { padding:10px;  color:white; background-color:#151515; width: 190px; height: 40px;        position:fixed; bottom:10px; right:415px; z-index:660; opacity:0.4; border-radius:5px; }';
        stringForStyles += '#search-text { width: 170px; color: #000000; position:fixed; bottom:20px; right:425px; z-index:669; opacity:0.9; border-radius:5px; }';
        stringForStyles += '#filterContainer {padding:10px;  color:white; background-color:#151515; width: 285px; height: 40px; position:fixed; bottom:10px; right:120px; z-index:660; opacity:0.4; border-radius:5px;} ';

        stringForStyles += '.myTooltip > span{';
        stringForStyles += '    background:url("http://vanderbiltent.wikispaces.com/file/view/black_arrow2.gif/512342574/black_arrow2.gif");';
        stringForStyles += '    background-size:270px 110px;';
        stringForStyles += '    background-repeat:no-repeat;';
        stringForStyles += '    width:270px;';
        stringForStyles += '    height:110px;';
        stringForStyles += '    position:absolute;';
        stringForStyles += '    text-align:center;';
        stringForStyles += '    vertical-align:middle;';
        stringForStyles += '    line-height: 1em;';
        stringForStyles += '    padding: 20px 0px 13px 0px;';
        stringForStyles += '    display:none;  ';
        stringForStyles += '    top:-100px;';
        stringForStyles += '    color:white;';
        stringForStyles += '    font-family: sans-serif;';
        stringForStyles += '    z-index: 10001;';
        stringForStyles += '}';

        stringForStyles += '.myTooltip {';
        stringForStyles += '    position:fixed; ';
        stringForStyles += '    bottom:70px;';
        stringForStyles += '    z-index: 10000;';
         
        stringForStyles += '}';

        stringForStyles += '#toolTipSearch > span{';
        stringForStyles += '    background-size:230px 70px;';
        stringForStyles += '    width:230px;';
        stringForStyles += '    height:70px;';
        stringForStyles += '    top:-100px;';
        stringForStyles += '    margin: 37px 0 0 0;';
        stringForStyles += '    padding: 25px 0px 13px 0px;';

        stringForStyles += '}';


        stringForStyles += '#toolTipSearch      { right:627px; } ';
        stringForStyles += '#toolTipBooks       { right:503px; } ';
        stringForStyles += '#toolTipNotes       { right:433px; } ';
        stringForStyles += '#toolTipOther       { right:363px; } ';
        stringForStyles += '#toolTipVideos      { right:293px; } ';
        
        stringForStyles += '.filterButton{';
        stringForStyles += '    cursor:pointer; ';
        stringForStyles += '    font-size: 1em;'; 
        stringForStyles += '    color:white; ';
        stringForStyles += '    text-align: center;';
        stringForStyles += '    line-height:30px;';
        stringForStyles += '    height: 30px;  /* was 30px */ '; 
        stringForStyles += '    width: 55px; /* was 55px */ ';
        stringForStyles += '    background-color:#088A29; ';
        stringForStyles += '    position:fixed; ';
        stringForStyles += '    bottom:15px; ';
        stringForStyles += '    z-index:999; ';
        stringForStyles += '    opacity:0.9; ';
        stringForStyles += '    border-radius:5px;';
        stringForStyles += '}';

        stringForStyles += '#filterBooks        { right:340px; } ';
        stringForStyles += '#filterNotes        { right:270px; } ';
        stringForStyles += '#filterOther        { right:200px; } ';
        stringForStyles += '#filterVideos       { right:130px; } ';


        stringForStyles += '#toc_container p a:link {color:#000000; text-decoration:none;} ';
        stringForStyles += '#toc_container p a:visited {color:#000000;}';
        stringForStyles += '#toc_container p a:hover {color:#0101DF; text-decoration:underline;}';
        stringForStyles += '.my_link_content > a:link {color:#000000 !important; text-decoration:none;}';
        stringForStyles += '.my_link_content > a:visited {color:#000000 !important;}';
        stringForStyles += '.my_link_content > a:hover {color:#000000 !important; text-decoration:none;}';
        stringForStyles += '.commentContainer > div > div > div > a:link {color:#0000FF;}   ';
        stringForStyles += '.commentContainer > div > div > div > a:visited {color:#0000FF;} ';
        stringForStyles += '.commentContainer > div > div > div > a:hover {color:#0000CD;}  ';
        stringForStyles += '.divider {  height: 0;  border-top: 1px solid #BDBDBD;  padding-bottom:1px;  margin-bottom:10px;  }';
        stringForStyles += '.box_background { background-color: #E6E6E6;  background-color: #F2F2F2;  background-color: #FAFAFA;  border: 1px solid #D8D8D8;  border-radius: 10px; margin: 0px 0 5px 0;  padding-bottom: 5px;}';
        stringForStyles += '.box_background p {  font-size: 11pt;  color: #A4A4A4;  margin: 0px 0 -5px 19px;  }';
        stringForStyles += '.box_background > ul > li:first-child {margin-top: 0px;  }';
        stringForStyles += '.box_background > ul > li:last-child {margin-bottom: 0px; }';
        stringForStyles += '.level1{margin-left:1.5em; }';
        stringForStyles += '.level2{margin-left:4.5em; }';
        stringForStyles += '.level3{margin-left:7.5em; }       ';

        stringForStyles += 'div[class*="tocColumn"] {    padding:7px 7px 15px 7px;  }';
        stringForStyles += '.columnContainer {  float:left;  display: inline-block;  background-color:#E6E6E6;  border: 1px solid #D8D8D8;  max-width: 20%;  }';
        stringForStyles += '.clearFloat{  clear: both;  float:none;  }';

        stringForStyles += '#firstCol{  border-top-left-radius:10px;  border-bottom-left-radius:10px;  }';
        stringForStyles += '#lastCol{  border-top-right-radius:10px;  border-bottom-right-radius:10px;  }';

        stringForStyles += '#nav_bar_container {height: 60px; width: 700px; background-color:#E6E6E6;  border: 1px solid #D8D8D8; position:fixed; top:-72px; left:5px; z-index:666; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px;}';
        stringForStyles += '#menu_item {font-family: sans-serif; font-size: 2.2em; font-weight: bold;  height:50px; width: 200px; background-color: none ; color: #000000 ; z-index: 700; position:absolute; top: 0px; left: 20px; }';

        stringForStyles += '#my_link_content h1 a::before { content:""; display:block; margin-top: -50px;  padding-top: 50px; }';
        stringForStyles += '#my_link_content div h2 a::before { content:""; display:block; margin-top: -90px;  padding-top: 90px; }';
        stringForStyles += '#my_link_content div h3 a::before { content:""; display:block; margin-top: -90px;  padding-top: 90px; }';
        stringForStyles += '#my_link_content div h4 a::before { content:""; display:block; margin-top: -90px;  padding-top: 90px; }';
        stringForStyles += '#my_link_content div h5 a::before { content:""; display:block; margin-top: -90px;  padding-top: 90px; }';
        stringForStyles += '#my_link_content div h6 a::before { content:""; display:block; margin-top: -90px;  padding-top: 90px; }';

        stringForStyles += 'a.clearSearch {';
        stringForStyles += '    background: url("http://vanderbiltent.wikispaces.com/file/view/closeButton.png/512342586/closeButton.png"); ';
        stringForStyles += '    background-size:16px 16px;';
        stringForStyles += '    background-repeat:no-repeat;';
        stringForStyles += '    background-position: center center;';
        stringForStyles += '    cursor: pointer;';
        stringForStyles += '    position: fixed;';
        stringForStyles += '    bottom: 22px;';
        stringForStyles += '    right: 427px;   ';
        stringForStyles += '    height: 16px;';
        stringForStyles += '    width: 16px;';
        stringForStyles += '    z-index: 10001;';
        stringForStyles += '}';

        stringForStyles += '</style>';
        $('head').append(stringForStyles);

// checks to see if user is on a iPad. if so, will change format
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
  var isAndroid = navigator.userAgent.match(/Android/i) != null;
  
  if(isiPad) // FORMAT HERE FOR IPAD
    { 
        stringForStyles = '<style type="text/css">';
        stringForStyles += '.tocHeader{  font-size:1.0em !important;  font-weight:bold !important;  text-align:center !important;  background-color:#BDBDBD;  border-radius:5px;  padding:4px 0px 3px 0px !important;  margin:0px 0px 10px 0px !important;  }';
        stringForStyles += '.tocLevel1{  font-size:0.9em !important;  font-weight:bold !important;  padding:0 0 0 0.1em !important;   margin: 0.6em 0 0 0 !important;  line-height:1.1 !important;  }';
        stringForStyles += '.tocLevel2{  font-size:0.8em !important;  padding:0 0 0 0.6em !important;  margin: 0.5em 0 0 0 !important;  line-height:1.1 !important;  }';
        stringForStyles += '.tocLevel3{  font-size:0.75em !important;  font-weight:700; padding:0 0 0 1.3em !important;  margin: 0.5em 0 0 0 !important;  line-height:1.1 !important;  }';
        stringForStyles += '#search-text { width: 170px; color: #000000; position:fixed; bottom:17px; right:425px; z-index:669; opacity:0.9; border-radius:5px; }';
        stringForStyles += 'div[class*="tocColumn"] {    padding:4px 4px 10px 4px;  }';
        stringForStyles += '</style>';
        $('head').append(stringForStyles);
    } 
		else if (isiPhone || isAndroid)  // FORMAT HERE FOR IPHONE AND ANDROID
    { 
        stringForStyles = '<style type="text/css">';
        stringForStyles += '.tocHeader{  font-size:0.7em !important;  font-weight:bold !important;  text-align:center !important;  background-color:#BDBDBD;  border-radius:5px;  padding:2px 0px 2px 0px !important;  margin:0px 0px 2px 0px !important;  }';
        stringForStyles += '.tocLevel1{  font-size:0.6em !important;  font-weight:bold !important;  padding:0 0 0 0.1em !important;   margin: 0.6em 0 0 0 !important;  line-height:1.1 !important;  }';
        stringForStyles += '.tocLevel2{  font-size:0.5em !important;  padding:0 0 0 0.4em !important;  margin: 0.5em 0 0 0 !important;  line-height:1.1 !important;  }';
        stringForStyles += '.tocLevel3{  font-size:0.5em !important;  font-weight:700; padding:0 0 0 0.8em !important;  margin: 0.5em 0 0 0 !important;  line-height:1.1 !important;  }';

              stringForStyles += 'div[class*="tocColumn"] {      padding:4px 2px 7px 2px;  }';
        stringForStyles += '#filterContainer {padding:10px;  color:white; background-color:#151515; width: 285px; height: 40px; position:fixed; bottom:5px; right:120px; z-index:660; opacity:0.4; border-radius:5px;} ';
        stringForStyles += 'a.backToTop:link, a.backToTop:visited {padding:0 10px 0 10px; font-size: 1em; color:white !important; background-color:#151515; display:block; text-decoration:none; width 15px; height: 40px; line-height: 40px; position:fixed; bottom:5px; right:20px; z-index:1666 !important; opacity:0.4; border-radius:5px;} ';

        stringForStyles += '.filterButton{';
        stringForStyles += '    cursor:pointer; ';
        stringForStyles += '    font-size: 1em;'; 
        stringForStyles += '    color:white; ';
        stringForStyles += '    text-align: center;';
        stringForStyles += '    line-height:30px;';
        stringForStyles += '    height: 30px;  /* was 30px */ '; 
        stringForStyles += '    width: 55px; /* was 55px */ ';
        stringForStyles += '    background-color:#088A29; ';
        stringForStyles += '    position:fixed; ';
        stringForStyles += '    bottom:10px; ';
        stringForStyles += '    z-index:999; ';
        stringForStyles += '    opacity:0.9; ';
        stringForStyles += '    border-radius:5px;';
        stringForStyles += '}';
        
        stringForStyles += '#searchContainer { bottom:-500px; }';
        stringForStyles += '#search-text { bottom:-500px;}';
        stringForStyles += 'a.clearSearch { bottom:-500px; }';

        stringForStyles += '</style>';
        $('head').append(stringForStyles);
    } 
		else // THIS IS THE CODE FOR THE DESKTOP FORMATTING (I.E., NOT IPAD OR IPHONE)
    {  
         stringForStyles = '<style type="text/css">';
         stringForStyles += '.columnContainer div[class*="tocColumn"] .tocHeader{  font-size:1.7em;  word-wrap:break-word; font-weight:bold    ;  text-align:center    ;  background-color:#BDBDBD;  border-radius:5px;  padding:0px 0px 0px 0px;  margin:0 0 0.3em 0;  }';
         stringForStyles += '.columnContainer div[class*="tocColumn"] .tocHeader a{  word-wrap: break-word;}';
         stringForStyles += '.columnContainer div[class*="tocColumn"] .tocLevel1{  font-size:1.15em;   padding:0 0 0 0.4em  ;  margin: 0.9em 0 0.5em 0    ;  line-height:1.1    ;  word-wrap: break-word; font-weight:bold    ;   }';
         stringForStyles += '.columnContainer div[class*="tocColumn"] .tocLevel2{  font-size:1.1em;   padding:0 0 0 1.3em  ;  margin: 0.2em 0 0.6em 0    ;  line-height:1.1    ;  word-wrap: break-word; }';
         stringForStyles += '.columnContainer div[class*="tocColumn"] .tocLevel3{  font-size:0.95em;  padding:0 0 0 2.7em  ;  margin: 0.5em 0 0.7em 0    ;  line-height:1.1    ;  word-wrap: break-word;  font-weight:700;  }';
         stringForStyles += '</style>';
        $('head').append(stringForStyles);
    };

// -------------------------------------------------------------------------------------------------
// ----------------------------------------- TOOL TIPS ---------------------------------------------
// -------------------------------------------------------------------------------------------------
	
  // only allows the tool tips if it is NOT an iPad or iPhone
  if (!isiPad && !isiPhone && !isAndroid){
      $('.filterButton, #search-text').hover(
                        function(){
                                $(this).next().animate({opacity:'show', top: '-80'}, 'slow');
                        },

                        function(){
                                $(this).next().animate({opacity:'hide', top: '-100'}, 'fast');
                        }
                );
 };

// -------------------------------------------------------------------------------------------------
// -------------------------------------------- TOC ------------------------------------------------
// -------------------------------------------------------------------------------------------------

 // create and format the columns for the TOC
        for (var i = 1; i <= numColumns; i++){
                if ( i === 1 ){
                        $("#toc_container").append("<div class='columnContainer' id='firstCol'><div class='tocColumn" + i + "'></div></div>");
                } else if (i != 1 && i != numColumns){
                        $("#toc_container").append("<div class='columnContainer'><div class='tocColumn" + i + "'></div></div>");
                } else if (i = numColumns){
                        $("#toc_container").append("<div class='columnContainer' id='lastCol'><div class='tocColumn" + i + "'></div></div>");
                } else {
                        alert("col not found");
                }
        }
        
        // sets column widths of the TOC based on number of columns
        var colWidth = (100) / numColumns;
        $(".columnContainer").css( "width", colWidth + "%" );  
        
        // clearFloat needed to ensure everything below TOC is on a new line
        $("#toc_container").after("<div class='clearFloat'></div>");
        
        // populates TOC based on the headers in the HTML and gives them links to anchors
        var headerCounter = 0;
		var linkCounter = 0;
        $('.commentContainer h1, h2, h3, h4, h5, h6').each(function(){          
                var headerType = $(this).prop("nodeName");
                switch(headerType){
                        case 'H1':
                                headerCounter++; 
				                linkCounter++;
                                $(".tocColumn" + headerCounter + "").
                                append("<p class='tocHeader'><a href='#" + linkCounter + "'>" + $(this).text() + "</a></p>");
                                break;
                        case 'H2':
                        case 'H3':
                        case 'H4':
				                linkCounter++;
                                $(".tocColumn" + headerCounter + "").
                                append("<p class='tocLevel1'><a href=#" + linkCounter + ">" + $(this).text() + "</a></p>");
                                break;
                        case 'H5':
				                linkCounter++;
                                $(".tocColumn" + headerCounter + "").
                                append("<p class='tocLevel2'><a href=#" + linkCounter + ">" + $(this).text() + "</a></p>");
                                break;
                        case 'H6':
				                linkCounter++;
                                $(".tocColumn" + headerCounter + "").
                                append("<p class='tocLevel3'><a href=#" + linkCounter + ">" + $(this).text() + "</a></p>");
                                break;
                }
        });
        
        // makes the TOC columns the same height
        var maxHeight;
				function makeColsSameHeight() {
                maxHeight = Math.max.apply(null, $('div[class*="tocColumn"]').map(function(){
                        return $(this).height();
                        alert($(this).height());
                }).get())+20+'px';
                $('div[class*="tocColumn"]').each(function() {
                        $(this).css({'height':maxHeight});
                });
        }
        makeColsSameHeight();
				function makeTOCHeadersSameHeight() {
                maxHeight = Math.max.apply(null, $('p[class*="tocHeader"]').map(function(){
                        return $(this).height();
                        alert($(this).height());
                }).get())+5 +'px';
                $('p[class*="tocHeader"]').each(function() {
                        $(this).css({'height':maxHeight});
                });

        }
        makeTOCHeadersSameHeight();

        //adds links to the table of contents based upon the header contents
        var li_counter = 0;
		linkCounter = 0;
        $('.commentContainer h1, h2, h3, h4, h5, h6').each(function(){
                linkCounter++;
                $(this).append('<a name="' + linkCounter + '" /></a.');  
        });

// -------------------------------------------------------------------------------------------------
// ---------------------------------- SECTION BACKGROUNDS ------------------------------------------
// -------------------------------------------------------------------------------------------------
        // changes background color slightly when section is moused over
        $('.box_background').mouseenter(function(){     
                $(this).css('background-color', '#F2F2F2');     
                $(this).find('p').css('color', '#6E6E6E');
        });
        $('.box_background').mouseleave(function(){
            $(this).css('background-color', '#FAFAFA');
            $(this).find('p').css('color', '#A4A4A4');
        });
        
// -------------------------------------------------------------------------------------------------
// --------------------------------------- FILTER BUTTONS ------------------------------------------
// -------------------------------------------------------------------------------------------------
        
function checkToSeeIfAllHidden(){
        // books
                        var bookLIs = $('.wikiPage > div .books').find('li');
                        var hiddenBookLIs = $('.wikiPage > div .books').find('.hidden');
                        if ($(bookLIs).length !== $(hiddenBookLIs).length ){
                                hiddenBooks = false;
                        } else {
                                hiddenBooks = true;
                        };
        // other
                        var otherLIs = $('.wikiPage > div .other').find('li');
                        var hiddenOtherLIs = $('.wikiPage > div .other').find('.hidden');
                        if ($(otherLIs).length !== $(hiddenOtherLIs).length ){
                                hiddenOther = false;
                        } else {
                                hiddenOther = true;
                        };
        // notes
                        var notesLIs = $('.wikiPage > div .notes').find('li');
                        var hiddenNotesLIs = $('.wikiPage > div .notes').find('.hidden');
                        if ($(notesLIs).length !== $(hiddenNotesLIs).length  ){
                                hiddenNotes = false;
                        } else {
                                hiddenNotes = true;
                        };      
        // video
                        var videoLIs = $('.wikiPage > div .videos').find('li');
                        var hiddenVideoLIs = $('.wikiPage > div .videos').find('.hidden');
                        if ($(videoLIs).length !== $(hiddenVideoLIs).length){
                                hiddenVideos = false;
                        } else {
                                hiddenVideos = true;
                        };
};


        function checkButtonColors(){
                if (hiddenBooks === true){
                        $('#filterBooks').css('background-color', '#8A0808');  //red color
                } else {
                        $('#filterBooks').css('background-color', '#088A29'); //green color                     
                }
                
                if (hiddenOther === true){
                        $('#filterOther').css('background-color', '#8A0808');  //red color
                } else {
                        $('#filterOther').css('background-color', '#088A29'); //green color                     
                }

                if (hiddenNotes === true){
                        $('#filterNotes').css('background-color', '#8A0808');  //red color
                } else {
                        $('#filterNotes').css('background-color', '#088A29'); //green color                     
                }
                
                if (hiddenVideos === true){
                        $('#filterVideos').css('background-color', '#8A0808');  //red color
                } else {
                        $('#filterVideos').css('background-color', '#088A29'); //green color                    
                }
        };
        
        // changes color of bottom right buttons slightly when  moused over

                $('#filterBooks').hover(
                        function(){
                                if (hiddenBooks === true){ $('#filterBooks').css('background-color', '#B40404');  } //red color 
									else { $('#filterBooks').css('background-color', '#04B431'); }; //green color  
                        },
                        function(){
                                if (hiddenBooks === true){ $('#filterBooks').css('background-color', '#8A0808');  } //red color
									else { $('#filterBooks').css('background-color', '#088A29'); } //green color                     
                        }
                );
                
                $('#filterOther').hover(
                        function(){
                                if (hiddenOther === true){ $('#filterOther').css('background-color', '#B40404'); } //red color
									else { $('#filterOther').css('background-color', '#04B431'); }; //green color
                        },
                        function(){ 
								if (hiddenOther === true){ $('#filterOther').css('background-color', '#8A0808'); } //red color
									else { $('#filterOther').css('background-color', '#088A29'); } //green color
                        }
                );
        
                $('#filterNotes').hover(
                        function(){
                                if (hiddenNotes === true){ $('#filterNotes').css('background-color', '#B40404'); } //red color
									else { $('#filterNotes').css('background-color', '#04B431'); }; //green color
                        },
                        function(){
                                if (hiddenNotes === true){ $('#filterNotes').css('background-color', '#8A0808');  } //red color 
									else { $('#filterNotes').css('background-color', '#088A29'); } //green color 
                        }
                );              

                $('#filterVideos').hover(
                        function(){
                                if (hiddenVideos === true){ $('#filterVideos').css('background-color', '#B40404'); } //red color 
									else { $('#filterVideos').css('background-color', '#04B431'); }; //green color 
                       },
                        function(){
                                if (hiddenVideos === true){ $('#filterVideos').css('background-color', '#8A0808');  } //red color 
									else { $('#filterVideos').css('background-color', '#088A29'); } //green color 
                        }
                );
        

        
        
        //quick filters provided by the buttons on the lower right of the screen
        $('#filterBooks').click(function(){     
                if (!$('.books').hasClass('hidden')){
                        $('.books li').addClass('hidden');      
                        $('.books')     
                        $(this).css('background-color', '#8A0808');
                } else {
                        $('.books li').removeClass('hidden');
                        $('.books').removeClass('hidden');
                        $(this).css('background-color', '#088A29');
                }
                hideHeadersAndBoxes();
                checkToSeeIfAllHidden();
                checkButtonColors();
                document.getElementById("search-text").value = "";
                if(!$('html, body').is(':animated')){
                     $('html, body').animate({
                           scrollTop: $('#my_link_content').offset().top - 30
                     }, 500);
                };                
                
        });

        $('#filterNotes').click(function(){     
                if (!$('.notes').hasClass('hidden')){
                        $('.notes').addClass('hidden'); 
                        $('.notes li').addClass('hidden');      
                        $(this).css('background-color', '#8A0808');
                } else {
                        $('.notes').removeClass('hidden');
                        $('.notes li').removeClass('hidden');
                        $(this).css('background-color', '#088A29');
                }
                hideHeadersAndBoxes();
                checkToSeeIfAllHidden();
                checkButtonColors();
                document.getElementById("search-text").value = "";
                if(!$('html, body').is(':animated')){
                     $('html, body').animate({
                           scrollTop: $('#my_link_content').offset().top - 30
                     }, 500);
                };                
                
        });     
        
        $('#filterOther').click(function(){     
                if (!$('.other').hasClass('hidden')){
                        $('.other').addClass('hidden');
                        $('.other li').addClass('hidden');
                        $(this).css('background-color', '#8A0808');
                } else {
                        $('.other').removeClass('hidden');
                        $('.other li').removeClass('hidden');
                        $(this).css('background-color', '#088A29');
                }
                hideHeadersAndBoxes();
                checkToSeeIfAllHidden();
                checkButtonColors();
                document.getElementById("search-text").value = "";
                if(!$('html, body').is(':animated')){
                     $('html, body').animate({
                           scrollTop: $('#my_link_content').offset().top - 30
                     }, 500);
                };                

        });     
        
        $('#filterVideos').click(function(){    
                if (!$('.videos').hasClass('hidden')){
                        $('.videos').addClass('hidden');        
                        $('.videos li').addClass('hidden');     
                        $(this).css('background-color', '#8A0808');
                } else {
                        $('.videos').removeClass('hidden');
                        $('.videos li').removeClass('hidden');
                        $(this).css('background-color', '#088A29');
                }
                hideHeadersAndBoxes();
                checkToSeeIfAllHidden();
                checkButtonColors();
                document.getElementById("search-text").value = "";
                if(!$('html, body').is(':animated')){
                     $('html, body').animate({
                           scrollTop: $('#my_link_content').offset().top - 30
                     }, 500);
                };                

         });

        
// -------------------------------------------------------------------------------------------------
// ----------------------------------------- SEARCH BOX --------------------------------------------
// -------------------------------------------------------------------------------------------------
	 //we want this function to fire whenever the user types in the search-box
	 $("#search-text").keyup(function () {
			//first we create a variable for the value from the search-box
			var searchTerm = $("#search-text").val();
			//then a variable for the list-items (to keep things clean)
			//  var listItem = $('#list').children('li');
			var listItem = $('.commentContainer div ul').find('li');
			
			//extends the default :contains functionality to be case insensitive
			//if you want case sensitive search, just remove this next chunk
			$.extend($.expr[':'], {
			  'containsi': function(elem, i, match, array)
			  {
				return (elem.textContent || elem.innerText || '').toLowerCase()
				.indexOf((match[3] || "").toLowerCase()) >= 0;
			  }
			});//end of case insensitive chunk

			//this part is optional
			//here we are replacing the spaces with another :contains
			//what this does is to make the search less exact by searching all words and not full strings
			var searchSplit = searchTerm.replace(/ /g, "'):containsi('")
						
			//here is the meat. We are searching the list based on the search terms
			$(".commentContainer div ul li").not(":containsi('" + searchSplit + "')").each(function(e)   {
				  $(this).addClass('hidden');
			});
		
			//this does the opposite -- brings items back into view
			$(".commentContainer div ul li:containsi('" + searchSplit + "')").each(function(e) {
				  $(this).removeClass('hidden');
			});

				// hides all the headers and boxes associated with list items that have already been hidden by the search box.
				hideHeadersAndBoxes();
				checkToSeeIfAllHidden();
				checkButtonColors();

	
		if($("#search-text").val() !== ""){
				if(!$('html, body').is(':animated')){
				$('html, body').animate({
					scrollTop: $('#my_link_content').offset().top - 30
				}, 500);
			};
		};		
		
		
		});

        
        // clears the search box when the x-button is pressed
        $('.clearSearch').click(function(){     
                document.getElementById("search-text").value = "";
                $("#search-text").keyup();
        });
        // clears the search box when the esc key is pressed
        $('input[type="text"]').keyup(function(e){
        if(e.keyCode == 27) {
            $(this).val('');
                $("#search-text").keyup();
        }
    });

	
// -------------------------------------------------------------------------------------------------
// --------------------------------------- HIDE FUNCTIONS ------------------------------------------
// -------------------------------------------------------------------------------------------------
        // hides all the headers and boxes associated with list items that have already been hidden by the search box.
        function hideHeadersAndBoxes(){
                var allLiElementsForThisDiv;
                var numOfLiInDiv;
                var hiddenInDiv;
                var numOfHiddenInDiv;
                var hiddenCount = 0;
        
/*              $('.wikiPage > div').find('ul').each(function(){
                        numListItems = $(this).find('li').length;
                        numListItemsHidden = $(this).find('.hidden').length;
                        if (numListItems === numListItemsHidden){
                                //$(this).parent().hide();
                                $(this).parent().addClass('hidden');    
                        } else {
                                $(this).parent().removeClass('hidden');
                                //$(this).parent().show();
                        };
                
                });
*/
                $('#my_link_content div').each(function(){
                        hiddenCount = 0;
                        allLiElementsForThisDiv = $(this).find('li');
                        numOfLiInDiv = $(allLiElementsForThisDiv).length;
                        $(allLiElementsForThisDiv).each(function(){
                                if ($(this).hasClass('hidden')) {
                                        hiddenCount++;
                                };
                        });
                        if (!$(this).hasClass('divider') && numOfLiInDiv === hiddenCount){
                                $(this).addClass('hidden');
                        } else {
                                $(this).removeClass('hidden');
                        };
                });     
        }
        
        var TOCPosition_Left;
        var TOCPosition_Top;
        var TOCPosition_Bottom;
        var navBar_Left;
        var navBarWidth;
        var navBarOffset = 20; //to hide the rounded corners at the top of the nav bar
        var navBarHeight = $('#nav_bar_container').height() - navBarOffset;
        var navBarEffectiveHeight = 70; 
        var closestDiv;
        var placeholder;
        var closestObject;
        var numberOfTocColumns;

        // update variables for TOC position
        TOCPosition_Left = $('#firstCol').offset().left;
        TOCPosition_Top = $('#firstCol').offset().top;
        TOCPosition_Bottom = TOCPosition_Top + $('#firstCol').height();
        // set nav bar left position
        navBar_Left = TOCPosition_Left;
        $('#nav_bar_container').css('left', navBar_Left + 'px');
        // set nav bar width
        navBarWidth = $('#toc_container').width();
        $('#nav_bar_container').width(navBarWidth);
        


// -------------------------------------------------------------------------------------------------
// --------------------------------- RESIZE TOC COLUMNS AND HEADER ---------------------------------
// -------------------------------------------------------------------------------------------------
		var initial_format_tocHeader_fontSize = $('.tocHeader').css('font-size');
		var initial_format_tocLevel1_fontSize = $('.tocLevel1').css('font-size');
		var initial_format_tocLevel2_fontSize = $('.tocLevel2').css('font-size');
		var initial_format_tocLevel3_fontSize = $('.tocLevel3').css('font-size');
		
		var initial_format_tocColumn_padding_left_right = $('div[class*="tocColumn"]').css('padding-left');
		var initial_format_tocLevel1_padding_left = $('.tocLevel1').css('padding-left');
		var initial_format_tocLevel2_padding_left = $('.tocLevel2').css('padding-left');
		var initial_format_tocLevel3_padding_left = $('.tocLevel3').css('padding-left');

		initial_format_tocHeader_fontSize = initial_format_tocHeader_fontSize.replace("px","");
		initial_format_tocLevel1_fontSize = initial_format_tocLevel1_fontSize.replace("px","");
		initial_format_tocLevel2_fontSize = initial_format_tocLevel2_fontSize.replace("px","");
		initial_format_tocLevel3_fontSize = initial_format_tocLevel3_fontSize.replace("px","");

		initial_format_tocColumn_padding_left_right = initial_format_tocColumn_padding_left_right.replace("px","");
		initial_format_tocLevel1_padding_left = initial_format_tocLevel1_padding_left.replace("px","");
		initial_format_tocLevel2_padding_left = initial_format_tocLevel2_padding_left.replace("px","");
		initial_format_tocLevel3_padding_left = initial_format_tocLevel3_padding_left.replace("px","");
		
        $(window).resize(function(){
				// change fonts and indents based on window size

					// set nav bar width
					navBarWidth = $('#toc_container').width();
					$('#nav_bar_container').width(navBarWidth);
					// tocColumn padding
					var new_padding = initial_format_tocColumn_padding_left_right * (Math.min(navBarWidth,1400)/1400);
					$('div[class*="tocColumn"]').css('padding-left', new_padding + 'px');
					$('div[class*="tocColumn"]').css('padding-right', new_padding + 'px');
					
					// tocHeader font and padding
					var new_font_size = initial_format_tocHeader_fontSize * .3 + initial_format_tocHeader_fontSize * .7 *(Math.min(navBarWidth,1400)/1400);
					$('.tocHeader').css('font-size', new_font_size + 'px');
					$('.tocHeader').css('line-height', new_font_size + 'px');
					$('.tocHeader').css('padding-top', new_font_size/1.5 + 'px');
					$('.tocHeader').css('padding-bottom', new_font_size/4 + 'px');

					// tocLevel1 font and padding
					var new_font_size = initial_format_tocLevel1_fontSize * .4 + initial_format_tocLevel1_fontSize * .6 *(Math.min(navBarWidth,1200)/1200);
					$('.tocLevel1').css('font-size', new_font_size + 'px');
					new_padding = initial_format_tocLevel1_padding_left * (Math.min(navBarWidth,1400)/1400);
					$('.columnContainer div[class*="tocColumn"] .tocLevel1').css('padding-left', new_padding + 'px');
					
					// toclevel2 font and padding
					var new_font_size = initial_format_tocLevel2_fontSize * .5 + initial_format_tocLevel2_fontSize * .5 *(Math.min(navBarWidth,1200)/1200);
					$('.tocLevel2').css('font-size', new_font_size + 'px');
					new_padding = initial_format_tocLevel2_padding_left * (Math.min(navBarWidth,1400)/1400);
					$('.columnContainer div[class*="tocColumn"] .tocLevel2').css('padding-left', new_padding + 'px');
					
					// toclevel3 font and padding
					var new_font_size = initial_format_tocLevel3_fontSize * .6 + initial_format_tocLevel3_fontSize * .4 *(Math.min(navBarWidth,1200)/1200);
					$('.tocLevel3').css('font-size', new_font_size + 'px');
					new_padding = initial_format_tocLevel3_padding_left * (Math.min(navBarWidth,1400)/1400);
					$('.columnContainer div[class*="tocColumn"] .tocLevel3').css('padding-left', new_padding + 'px');
					
					// update variables for TOC position
					TOCPosition_Left = $('#firstCol').offset().left;
					TOCPosition_Top = $('#firstCol').offset().top;
					TOCPosition_Bottom = TOCPosition_Top + $('#firstCol').height();

					// set nav bar left position
					navBar_Left = TOCPosition_Left;
					$('#nav_bar_container').css('left', navBar_Left + 'px');

					//find last item in longest TOC column
					numberOfTocColumns = $('.columnContainer').length;
					var maxColHeight = 0;
					var currentColHeight = 0;
					for (var i = 1; i <= numberOfTocColumns; i++){
							currentColHeight = 
							$('.tocColumn' + i + ' p:last-child').position().top + 
							$('.tocColumn' + i + ' p:last-child').height();
							if ( currentColHeight > maxColHeight ) {
									maxColHeight = currentColHeight;
							};
					};              
					//apply the max height to all the TOC columns
					for (var i = 1; i <= numberOfTocColumns; i++){
							$('.tocColumn' + i).height(maxColHeight);
					};
					//same things with TOC headers
					var maxColHeight = 0;
					var currentColHeight = 0;
					for (var i = 1; i <= numberOfTocColumns; i++){
							currentColHeight = $('.tocColumn' + i + ' .tocHeader a').height() + 5;
							if ( currentColHeight > maxColHeight ) {
									maxColHeight = currentColHeight;
							};
	//                        console.log($('.tocColumn' + i + ' .tocHeader a').height());
					};              
					//apply the max height to all the TOC columns
					for (var i = 1; i <= numberOfTocColumns; i++){
							$('.tocColumn' + i + ' .tocHeader').height(maxColHeight);
					};

        });


// -------------------------------------------------------------------------------------------------
// -------------------------------------- TOP NAV BAR ----------------------------------------------
// -------------------------------------------------------------------------------------------------
	
		// brings nav bar into view if it is below the TOC
        $(window).scroll(function(event) {

//              console.log("TOCPosition_Bottom = " + TOCPosition_Bottom);
//              console.log("$(#toc_container).height() = " + $('#toc_container').height());
                
                if (TOCPosition_Bottom < $(window).scrollTop()){
                        if(!$("#nav_bar_container").is(':animated')){
                                $('#nav_bar_container').animate({top: '-2px'});
                        };
                } else {
                        if(!$("#nav_bar_container").is(':animated')){
                                $('#nav_bar_container').animate({top: '-72px'});
                        };
                };
                
				// changes the nav bar text based on the h1 locations
//              console.log("$(window).scrollTop() = " + $(window).scrollTop()); //how far the top of the scroll window is from the top of the document
                placeholder = 0;
                $('h1[id*="menu_item_anchor"]').each(function(){
//                      console.log("$(this).offset().top = " + $(this).offset().top);
//                      console.log("$(this).prop(nodeName) = " + $(this).prop("nodeName"));
//                      console.log("$(this).text() = " + $(this).text());
                        if ($(this).offset().top < $(window).scrollTop()) {
                                if ($(this).offset().top > placeholder){
                                        closestObject = $(this);
                                };
                        };
                 });
//              console.log("closestObject = " + $(closestObject).length + " -- " + $(closestObject).text());
                 $('#menu_item').text($(closestObject).text());
                });
                
// -------------------------------------------------------------------------------------------------
// ---------------------CODE TO HANDLE LINKS WITH SEARCH QUERIES EMBEDDED---------------------------
// -------------------------------------------------------------------------------------------------
var currentPageSearchTerms = window.location.href;
var n = currentPageSearchTerms.indexOf("bytopicsearch");
var currentSearchTerm;

if (n != -1){
	currentSearchTerm = currentPageSearchTerms.substring(n + 14,currentPageSearchTerms.length);
  currentSearchTerm =  currentSearchTerm.replace("%20"," ");
	console.log ("currentSearchTerm = " + currentSearchTerm);
	document.getElementById("search-text").value = currentSearchTerm;
  $("#search-text").keyup(); 
	if($("#search-text").val() !== ""){
			if(!$('html, body').is(':animated')){
			$('html, body').animate({
				scrollTop: $('#my_link_content').offset().top - 30
			}, 500);
		};
	};	  
   
} else {
	console.log("NO search here");
};


// -------------------------------------------------------------------------------------------------
// -------------------------------------------- OTHER ----------------------------------------------
// -------------------------------------------------------------------------------------------------

        function convertNaNToNumber(val){
                if (isNaN(val)){
                        return 999;
                } else {
                        return val;x
                };
        };


                
    checkToSeeIfAllHidden();
    checkButtonColors();
	$(window).resize();

        
});     